package self.seon.expression;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import self.seon.domain.LoginUser;

import java.util.List;

/**
 * @author Seon
 * @date 2022/9/9 14:32
 */
@Slf4j
@Component("ex")
public class SeonExpressionRoot {

    public boolean hadAuthority(String authority) {
        // 获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        List<String> permissions = loginUser.getPermissions();
        // 判断当前的用户集合中是否有对应的authority
        return permissions.contains(authority);
    }

}
